<!--
 * @Author: HeAo
 * @Date: 2022-08-24 15:15:14
 * @LastEditTime: 2022-08-26 17:04:16
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \JavaScripe\day-13事件机制\06-事件委托.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <ul id="father">
    <li id="li1">去哪</li>
    <li id="li2">吃啥</li>
    <li id="li3">干啥</li>
  </ul>
  <script>
    // 一般情况下 分别获取三个子元素的节点
    var li1 = document.getElementById('li1')
    var li2 = document.getElementById('li2')
    var li3 = document.getElementById('li3')
    /*
    原理：利用事件冒泡，点击子、则会一直往祖宗传递，不论子是否有绑定事件
    执行：在父设置onclick，再判断传入的值，对其设置相对事件操作
    */
    var father = document.getElementById('father')
    father.addEventListener('click', function (event) {
      console.log(event.target.id);
      switch (event.target.id) {
        case 'li1':
          console.log('苏州');
          break;
        case 'li2':
          console.log('螺蛳粉，吃不着');
          break;
        case 'li3':
          console.log('敲代码');
          break;
        default:
          break;
      }
    })
  </script>
</body>



</html>